<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/13 0013
 * Time: 18:12
 */
namespace app\admin\controller;
use app\models\Bases;

class Index extends Base {
    /**
     * admin后台框架首页
     * */
    public function index(){
        $id = cookie('login');
        $admin  = new Bases('admin');
        $config = new Bases('config');
        $info = $admin->find(['id'=>$id]);
        $config['system_name']    = $config->value(['cname' => 'system_name'],'option');
        $config['system_version'] = $config->value(['cname' => 'system_version'],'option');
        //根据id或股登陆管理员具备的权限
        $join = [
            ['auth_group_access c','a.id=c.uid','left'],
            ['auth_group b' ,'c.group_id = b.id','left'],
        ];
        $where['a.id'] = ['=',$id];
        $fields = "a.*,b.rules";
        $ruleIds = $admin->getJoinOne("a",$join,$where,$fields);
        $auth_rule = new Bases('auth_rule');
        if($id == $admin::ROLE_ADMIN){
            $parentButton = $auth_rule->selectData("p_id = 0");
            $sonButton    = $auth_rule->selectData("p_id != 0 and is_show = 0");
        }else{
            $parentButton = $auth_rule->selectData("id in ({$ruleIds['rules']}) and p_id = 0");
            $sonButton    = $auth_rule->selectData("id in ({$ruleIds['rules']}) and p_id != 0 and is_show = 0");
        }
        foreach($parentButton as $k=>$v){
            foreach($sonButton as $k1=>$v1){
                if($v1['p_id'] == $v['id']){
                    $button[$k]['parentButton'] = $v;
                    $button[$k]['sonButton'][]  = $v1;
                }
            }
        }
        return view('index',['config'=>$config,'info'=>$info,'button'=>$button]);
    }

    /**
     * admin后台首页内容
     * */
    public function main(){
        $id = cookie('login');
        $admin  = new Bases('admin');
        $config = new Bases('config');
        $info = $admin->find(['id'=>$id]);
        $config['system_name']    = $config->value(['cname' => 'system_name'],'option');
        $config['system_version'] = $config->value(['cname' => 'system_version'],'option');
        $config['copyright']      = $config->value(['cname' => 'copyright'],'option');
        return view('main',['config'=>$config,'info'=>$info]);
    }

    /**
     * index控制器空方法
     * */
    public function _empty(){
        return view('Error/404');
    }
}
